package com.dragon.logical.service.auth.impl;

import cn.hutool.core.util.StrUtil;
import com.dragon.common.constant.CronJobConstant;
import com.dragon.logical.service.auth.TasksService;
import com.dragon.repository.entity.Tasks;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Service;


@Service
public class TasksServiceImpl implements TasksService {

    @Autowired
    Scheduler scheduler;

    @Override
    public void addSchedule(Tasks tasks) {
        try {
            JobDetail jobDetail = scheduler.getJobDetail(JobKey.jobKey(tasks.getJobName(), tasks.getJobGroupName()));
            if(jobDetail == null){
                Class className = Class.forName(tasks.getJobForName());
                QuartzJobBean jobBean = (QuartzJobBean) className.newInstance();
                jobDetail = JobBuilder.newJob(jobBean.getClass())
                        .withIdentity(tasks.getJobName(), tasks.getJobGroupName())
                        .storeDurably()
                        .build();
            }
            //什么时候开始，什么时候结束
            CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                    .forJob(jobDetail.getKey().getName(),jobDetail.getKey().getGroup())
                    .withIdentity(tasks.getTriggerName(), tasks.getTriggerGroup())
                    .withSchedule(CronScheduleBuilder.cronSchedule(tasks.getCronExpression()))
                    .build();
            if(tasks.getStartTime()!=null){
                cronTrigger.getTriggerBuilder().startAt(tasks.getStartTime());
            } else {
                cronTrigger.getTriggerBuilder().startNow();
            }
            //如果参数存在，则传递参数
            if(StrUtil.isNotBlank(tasks.getParams())){
                cronTrigger.getTriggerBuilder().usingJobData(CronJobConstant.REMINDER_JOB_DATA_MAP.variable(), tasks.getParams());
            }

            if(tasks.getEndTime()!=null){
                cronTrigger.getTriggerBuilder().endAt(tasks.getEndTime());
            }
            scheduler.addJob(jobDetail,true);
            scheduler.scheduleJob(cronTrigger);
//            scheduler.start();
        } catch (SchedulerException | ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void pauseSchedule(String jobName, String jobGroup) {
        try {
            scheduler.pauseJob(JobKey.jobKey(jobName, jobGroup));
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void pauseTrigger(String jobName, String jobGroup){
        try {
            scheduler.pauseTrigger(TriggerKey.triggerKey(jobName, jobGroup));
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteTrigger(String jobName, String jobGroup){
        try {
            scheduler.unscheduleJob(TriggerKey.triggerKey(jobName, jobGroup));
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void resumeSchedule(String jobName, String jobGroup) {
        try {
            scheduler.resumeJob(JobKey.jobKey(jobName, jobGroup));
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteSchedule(String jobName, String jobGroup) {
        try {
            scheduler.deleteJob(JobKey.jobKey(jobName, jobGroup));
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}
